package edu.fiu.cis.personalize.server.berkDB.serializers;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

import edu.fiu.cis.personalize.server.graph.Node;
import edu.fiu.cis.personalize.server.graph.store.NodeEdges;

public abstract class NodeSerializer<E extends Node> extends TupleBinding<E> {
	protected NodeEdgesSerializer edgesSerializer = new NodeEdgesSerializer();
	
	@Override
	public E entryToObject(TupleInput ti) {
		int id = ti.readInt();
		String gname = ti.readString();
		NodeEdges nodeEdges = edgesSerializer.entryToObject(ti);
		double rank = ti.readDouble();
		return process(ti,id,gname,nodeEdges,rank);
	}

	
	protected abstract E process(TupleInput ti,int id, String gname, NodeEdges nodeEdges, double rank) ;


	@Override
	public void objectToEntry(E node, TupleOutput to) {
		to.writeInt(node.getId());
		to.writeString(node.getStringKey());
		to.writeDouble(node.getRank());
		edgesSerializer.objectToEntry(node.getEdges(), to);
	}
}
